package com.zhangsan.senior1.week1.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhangsan.senior1.week1.domain.Job;
import com.zhangsan.senior1.week1.domain.Story;
import com.zhangsan.senior1.week1.service.JobSer;


@Controller
public class JobController {
	@Autowired
	private JobSer jobSer;
	
	@RequestMapping("list.do")
	public String list(Model model,@RequestParam(value="jname",required=false)String jname,@RequestParam(value="start",required=false)Integer start,@RequestParam(value="end",required=false)Integer end,@RequestParam(value="curr",defaultValue="1")int curr){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("jname", jname);
		map.put("start",start);
		map.put("end", end);
		PageHelper page=new PageHelper();
		page.startPage(curr, 2);
		List<Job> selectall = jobSer.selectall(map);
		PageInfo<Job> pageInfo=new PageInfo<Job>(selectall);
		model.addAttribute("page", pageInfo);
		return "list";
	}
	
	@RequestMapping("toadd.do")
	public String toadd(Model model){
		model.addAttribute("job", new Job());
		return "add";
	}
	
	@RequestMapping("add.do")
	public String add(Job job,BindingResult result){
		if(result.hasErrors()){
			List<ObjectError> allErrors = result.getAllErrors();
			System.out.println(allErrors);
			return "add";
		}
		jobSer.addone(job);
		return "redirect:list.do";
	}
	
	@RequestMapping("liststory.do")
	@ResponseBody
	public List<Story> liststory(Integer pid){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("pid", pid);
		List<Story> selectstory = jobSer.selectstory(map);
		return selectstory;
	}
	/**
	 * 
	* @Title: byone
	* @Description: 展示一条数据
	* @param id
	* @return
	* @return Job
	 */
	@RequestMapping("byone.do")
	@ResponseBody
	public Job byone(Integer id){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("jid", id);
		return jobSer.byone(map);
	}
	
	@RequestMapping("update.do")
	@ResponseBody
	public Job update(Integer id){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("jid", id);
		return jobSer.byone(map);
	}
}
